#include "function.h"
#include"stdShared.h"
#include<cstring>
#include<vector>
Function::Function(const stdTcpSocketPtr & clientInfo) 
{
    m_clientInfo = clientInfo;
}

Function::~Function() {}

bool Function::userIsExist(const char *username) 
{
    string sql = "SELECT COUNT(1) FROM userInfo where username='%s';";
    char insertSql[128] = {0};
    sprintf(insertSql, sql.c_str(), username);
    m_sqliteDB.connectDB("./test2.db");
    vector<vector<string>> res=m_sqliteDB.query(insertSql);
    m_sqliteDB.closeDB();
    if(res[0][0]=="0")
    {
        return false;
    }
    return true;
}

bool Function::userIsMatchPasswd(const char *username, const char *passwd)
{
    string sql = "SELECT COUNT(1) FROM userInfo where username='%s'and passwd='%s';";
    char insertSql[128] = {0};
    sprintf(insertSql, sql.c_str(), username,passwd);
    m_sqliteDB.connectDB("./test2.db");
    vector<vector<string>> res=m_sqliteDB.query(insertSql);
    m_sqliteDB.closeDB();
    if (res[0][0] == "0")
    {
        return false;
    }
    return true;
}

bool Function::userIsOnlined(const char *username)
{
    return true;
}

void Function::handleRegisterInfo(const Msg &msg)
{
    std::cout << "name:" << msg.name << std::endl;
    std::cout << "passwd:" << msg.passwd << std::endl;
    string username(msg.name);
    string passwd(msg.passwd);
    ReplyMsg responseMsg;
    memset(&responseMsg, 0, sizeof(responseMsg));
    responseMsg.type = REGESTER;
    if(userIsExist(msg.name))
    {
        responseMsg.state_code = REGESTER_USEREXIST;
    }
    else
    {
        responseMsg.state_code = REGESTER_SUCESS;
        string sql = "INSERT INTO userInfo(username,passwd) VALUES('%s','%s')";
        char insertSql[128] = {0};
        sprintf(insertSql, sql.c_str(), username.c_str(),passwd.c_str());
        m_sqliteDB.connectDB("./test2.db");
        m_sqliteDB.execute(insertSql);
        m_sqliteDB.closeDB();
    }
    //发送信息给客户端
    m_clientInfo->sendMessage(static_cast<const void *>(&responseMsg),sizeof(responseMsg));
}

void Function::handleLoginInfo(const Msg &msg) 
{
    std::cout << "name:" << msg.name << std::endl;
    std::cout << "passwd:" << msg.passwd << std::endl;

    string username(msg.name);
    string passwd(msg.passwd);
    ReplyMsg responseMsg;
    memset(&responseMsg, 0, sizeof(responseMsg));
    responseMsg.type = LOGIN;
    
    if (userIsMatchPasswd(username.c_str(),passwd.c_str())==true)
    {
            //登录成功
            responseMsg.state_code = LOGIN_SUCCESS;
        

    }
    else if(userIsExist(username.c_str())==false)
    {
        //没有该用户
        responseMsg.state_code = LOGIN_NOUSER;
    }
    else
    {
        //密码错误
        responseMsg.state_code = LOGIN_PASSWDERROR;
    }
    //发送信息给客户端
    m_clientInfo->sendMessage(static_cast<const void *>(&responseMsg),sizeof(responseMsg));
    #if 0
    // 判断用户名是否注册
    if(this->userIsExist(msg.name)==false)
    {
        //如果程序进入这里面，说明用户名不存在

        //将信息发送到客户端
    }
    else
    {
    // 如果用户名存在，判断用户名和密码是否匹配
    this->userIsMatchPasswd(msg.name, msg.passwd);
    // 判断用户是否已经登录
    this->userIsOnlined(msg.name);
    }
    #endif
}

void Function::handlAddFriendInfo(const Msg &msg)
{
    cout << "addfrient" << endl;
}

void Function::handlDelFriendInfo(const Msg &msg)
{
}

void Function::handlFriendChat(const Msg &msg)
{
}

void Function::handlFriendList(const Msg &msg)
{
}

void Function::handlNewGroupInfo(const Msg &msg)
{
}

void Function::handlExitGroupInfo(const Msg &msg)
{
}

void Function::handlJoinGroup(const Msg &msg)
{
}

void Function::handlInviteGroup(const Msg &msg)
{
}

void Function::handlChatGroup(const Msg &msg)
{
}
